<?php

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of CityDAO
 *
 * @author gaurav
 */


class CityDAO implements City {

    public function getCity() {

        // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();
        $sql = "SELECT ". Constant::$COL_ID. "," .Constant::$COL_STATE_ID .",".Constant::$COL_NAME." FROM ".Constant::$TABLE_CITY;
         FB::info("SQL --".$sql);
        $result = mysql_query($sql);
        $AllcityArray[] = Array();
        $i = 0;
        $row = mysql_fetch_assoc($result);
        if(count($row)!= 0){
        while($row = mysql_fetch_assoc($result)) {
            $tempCity = new CityDTO();
            $tempCity->setId($row['id']);
            $tempCity->setStateId($row['state_id']);
            $tempCity->setName($row['name']);

            $AllcityArray[$i] = $tempCity;
            if(Constant::$APPICATION_MODE == "D") {
                FB::info("SQL --".$sql);
                FB::info("id: ".$row['id']." State id : ".$row['state_id']." name : ".$row['name']);
            }
            $i++;
            //echo "<br />";
        } }
        return $AllcityArray;
    }  // getCity method ends here

    public function getCityByStateId($stateId) {

        // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();
        $sql = "SELECT ". Constant::$COL_ID. "," .Constant::$COL_STATE_ID .",".Constant::$COL_NAME." FROM ".Constant::$TABLE_CITY . "  where ".Constant::$COL_STATE_ID."= ".$stateId;

        $result = mysql_query($sql);
        $AllcityArray[] = Array();

        $i = 0;
         $row = mysql_fetch_assoc($result);
        if(count($row)!= 0){
        while($row = mysql_fetch_assoc($result)) {
            $tempCity = new CityDTO();
            $tempCity->setId($row['id']);
            $tempCity->setStateId($row['state_id']);
            $tempCity->setName($row['name']);
            $AllcityArray[$i] = $tempCity;
            if(Constant::$APPICATION_MODE == "D") {
                FB::info("SQL --".$sql);
                FB::info("id: ".$row['id']." State id : ".$row['state_id']." name : ".$row['name']);
            }

            //echo "<br />";
            $i++;
        } }else{ $AllcityArray = null;}
        return $AllcityArray;

    } // to get city by State By
    public function getCityById($cityId) {

        // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();
        $sql = "SELECT ". Constant::$COL_ID. "," .Constant::$COL_STATE_ID .",".Constant::$COL_NAME." FROM ".Constant::$TABLE_CITY . "  where ".Constant::$COL_ID."= ".$cityId;

        $result = mysql_query($sql);
        $AllcityArray[] = Array();
        while($row = mysql_fetch_assoc($result)) {
            $tempCity = new CityDTO();
            $tempCity->setId($row['id']);
            $tempCity->setStateId($row['state_id']);
            $tempCity->setName($row['name']);
            $AllcityArray[] = $tempCity;
            if(Constant::$APPICATION_MODE == "D") {
                FB::info("SQL --".$sql);
                FB::info("id: ".$row['id']." State id : ".$row['state_id']." name : ".$row['name']);
            }

            //echo "<br />";
        }
        mysql_close($connection);
        return $AllcityArray;
    } // to get City by City Id
    public function setCity($stateId,$cityName) {

        // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();
        //  $sql = "SELECT ". Constant::$COL_ID. "," .Constant::$COL_STATE_ID .",".Constant::$COL_NAME." FROM ".Constant::$TABLE_CITY . "  where ".Constant::$COL_ID."= ".$cityId;
        $sql = "INSERT INTO ".Constant::$TABLE_CITY." (".Constant::$COL_STATE_ID. ", ".Constant::$COL_NAME." ) VALUES ( '".$stateId."' , '".$cityName."')";

        $result = mysql_query($sql);
        if(Constant::$APPICATION_MODE == "D") {
            FB::info("SQL --".$sql);

        }

        mysql_close($connection);
    } // to insert the city name
    public function setCityById($cityId, $cityName) {
        // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();
        
        $sql = "UPDATE ".Constant::$TABLE_CITY." SET ".Constant::$COL_NAME." = '".$cityName."' WHERE ".Constant::$COL_ID." = '".$cityId."'";
        
        $result = mysql_query($sql);
         if(Constant::$APPICATION_MODE == "D") {
            FB::info("SQL --".$sql);

        }
        mysql_close($connection);
    } // to update the city
    public function removeCity() {

          // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();

        $sql = "DELETE FROM ".Constant::$TABLE_CITY."";

        $result = mysql_query($sql);
         if(Constant::$APPICATION_MODE == "D") {
            FB::info("SQL --".$sql);

        }
        mysql_close($connection);
    } // to delete all City
    public function removeCityById($cityId) {

          // to start logging on firebug tool
        ob_start();

        $connection =  DataBaseUtility::getConection();

        $sql = "DELETE FROM ".Constant::$TABLE_CITY." where ".Constant::$COL_ID." = ".$cityId."";

        $result = mysql_query($sql);
         if(Constant::$APPICATION_MODE == "D") {
            FB::info("SQL --".$sql);

        }
        mysql_close($connection);
    } // to remove the city by Id
}
?>
